
/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.projetinfoent.asyncmeteo;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.logging.Logger;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlSeeAlso;

/**
 * This class was generated by Apache CXF 2.4.2
 * 2011-10-16T18:23:09.217+02:00
 * Generated source version: 2.4.2
 * 
 */

@javax.jws.WebService(
		serviceName = "asyncMeteo",
		portName = "asyncMeteoSOAP",
		targetNamespace = "http://www.projetinfoent.com/asyncMeteo/",
		//wsdlLocation = "file:asyncMeteo.wsdl",
		endpointInterface = "com.projetinfoent.asyncmeteo.AsyncMeteo")

public class AsyncMeteoImpl implements AsyncMeteo, MessageListener {

	private static final Logger LOG = Logger.getLogger(AsyncMeteoImpl.class.getName());
	private static final HashMap<String,SituationMeteo> map = new HashMap<String,SituationMeteo>(); 
	private ConnectionFactory connectionFactory; 
	private Destination request, response;
	InitialContext jndiContext;
	Connection connection;
	Session session;
	public ConnectionFactory getConnectionFactory() {
		return connectionFactory;
	}
	public AsyncMeteoImpl(){

		try {
			// Initialise les attributs connectionFactory et destination.
			// Création d'un contexte JNDI
			jndiContext = new InitialContext();
			// Lookup de la fabrique de connexion et de la destination
			connectionFactory = (ConnectionFactory) jndiContext.lookup("connectionFactory"); 
			request = (Destination) jndiContext.lookup("Request");
			response = (Destination) jndiContext.lookup("Response");
			connection = connectionFactory.createConnection();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			// Création d'un MessageConsumer
			MessageConsumer consumer = session.createConsumer(response);
			// Reception des messages
			consumer.setMessageListener(this);
			connection.start();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see com.projetinfoent.asyncmeteo.AsyncMeteo#obtenirSituationMeteo(java.lang.String  idClient )*
	 */
	public com.projetinfoent.asyncmeteo.SituationMeteo obtenirSituationMeteo(java.lang.String idClient) { 
		LOG.info("Executing operation obtenirSituationMeteo");
		System.out.println(idClient);
		try {



			com.projetinfoent.asyncmeteo.SituationMeteo _return = null;
			if(this.map.containsKey(idClient)){
				_return = this.map.get(idClient);
			} else {
				System.out.println("WTF");
			}

			return _return;
		} catch (java.lang.Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	/* (non-Javadoc)
	 * @see com.projetinfoent.asyncmeteo.AsyncMeteo#deposerRequete(com.projetinfoent.asyncmeteo.RequeteMeteo  requete )*
	 */
	public void deposerRequete(RequeteMeteo requete) { 
		LOG.info("Executing operation deposerRequete");
		System.out.println(requete);
		try {
			// Création d'un MessageProducer
			MessageProducer producer = session.createProducer(request);
			connection.start();
			String out;
			StringWriter sw = new StringWriter();
			JAXBContext context;
			context = JAXBContext.newInstance("com.projetinfoent.asyncmeteo");
			Marshaller m = context.createMarshaller(); 
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(requete,sw);
			System.out.println(sw);

			TextMessage message = session.createTextMessage(sw.toString());
			producer.send(message);
		} catch (java.lang.Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	@Override
	public void onMessage(Message message) {

		TextMessage text = (TextMessage) message;
		SituationMeteo s;
		try {
			if (text instanceof TextMessage) {
				JAXBContext context;
				context = JAXBContext.newInstance("com.projetinfoent.asyncmeteo");
				System.out.println("Message: " + text.getText());
				Unmarshaller u = context.createUnmarshaller();

				s = (SituationMeteo) u.unmarshal(new StringReader(text.getText()));
				if (s != null){
					System.out.println ("Unmarshall réussi. Météo de "+  s.getReponseMeteo().getMeteo().getLieu() );
					System.out.println ("Vent  "+  s.getReponseMeteo().getMeteo().getVent() );
					System.out.println ("Temp  "+  s.getReponseMeteo().getMeteo().getTemperature() );
				}
				this.map.put(s.getRequeteMeteo().client, s);

			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
